﻿Public Class PTIC_NumFormatedTextBox

    Protected _format As String
    Protected _value As Double
    Protected _errorProvider As ErrorProvider

    Public Property FormatText() As String
        Get
            Return _format
        End Get
        Set(ByVal value As String)
            _format = value
            Text = Format(_value, FormatText)
        End Set
    End Property
    Public Property value() As Double
        Get
            Return _value
        End Get
        Set(ByVal value As Double)
            If value <> _value Then
                _value = value
                Text = Format(_value, FormatText)
            End If
        End Set
    End Property
    Public Property errorProvider() As ErrorProvider
        Get
            Return _errorProvider
        End Get
        Set(ByVal value As ErrorProvider)
            _errorProvider = value
        End Set
    End Property

    Protected Sub PTIC_TextBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
        If _errorProvider.GetError(Me) = "" Then
            Text = Format(value, "")
        End If
        Me.BackColor = Color.Yellow
        ''Me.
    End Sub
    Protected Sub PTIC_TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Return Then
            SendKeys.Send("{TAB}")
            e.Handled = True
        End If
    End Sub
    Protected Sub PTIC_NumeriqueTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Validating
        If Not IsNumeric(Me.Text) Then
            e.Cancel = True
            Me.Select(0, Me.Text.Length)
            _errorProvider.SetError(Me, "Une valeur numérique est attendue")
        End If
    End Sub
    Protected Sub PTIC_NumeriqueTextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Validated
        _errorProvider.SetError(Me, "")
        value = Me.Text
        Text = Format(_value, FormatText)
        Me.BackColor = Color.White
    End Sub

End Class
